﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Diagnostics.Contracts;
using System.Windows;

namespace ShredTheLog.Converters
{
    public sealed class RectConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null && values.Length == 2 && values[0] is double && values[1] is double)
            {
                var width = (double)values[0];
                var height = (double)values[1];
                if (width >= 0.0 && height >= 0.0)
                {
                    Contract.Assume(width >= 0.0);
                    Contract.Assume(height >= 0.0);
                    return new Rect(0.0, 0.0, width, height);
                }
                else
                {
                    return null;
                }
            }
            else
                return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        public static readonly RectConverter Default = new RectConverter();
    }
}
